<script setup>
import {User, Lock, View, Hide, Iphone} from '@element-plus/icons-vue'
import {ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
const router = useRouter();
const store = useStore();
const activeName = ref('first')
const checkCode = (rule, value, callback) => {
  if (activeName.value === 'first') {
    return callback()
  }
  if (!value) {
    return callback(new Error('请输入验证码'))
  }
  if (value.toLowerCase() !== code.value.toLowerCase()) {
    return callback(new Error('验证码错误'))
  }
  callback()
}
const rules = ref({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  code: [
    {
      required: true,
      validator: checkCode,
    }
  ]
})
const loginForm = ref({
  username: '',
  password: '',
  code: ''
})
const isHide = ref(true)
function handleRefreshCode() {
  code.value = (Math.random() * 8999 + 1000).toFixed(0).toString();
}
const loginFormRef = ref(null)
function login() {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      await store.dispatch('login', loginForm.value)
      await router.replace('/')
    } else {
      return false
    }
  })
}
const code = ref('')
const loginForm2 = ref({
  phone: '',
  code: ''
})
const rules2 = ref({
  phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
  code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
})
const loginFormRef2 = ref(null)
const login2 = () => {
  loginFormRef2.value.validate(async (valid) => {
    if (valid) {
      await router.replace({
        path: '/home'
      })
    } else {
      return false
    }
  })
}
const toRegister = () => {
  router.push({
    path: '/register'
  })
}
</script>


<template>
  <div class="login">
    <div class="login-form f-c a-c j-c">
      <div class="login-title">律师资讯平台</div>
      <div class="login-form-box f-c a-c j-c">
        <div class="name"> 用户登录</div>
        <div class="form-content f-r">
          <div class="form-icon"></div>
          <div class="f-g">
            <el-tabs v-model="activeName" class="demo-tabs">
              <el-tab-pane label="密码登录" name="first">
                <el-form ref="loginFormRef" label-position="top" size="large" :model="loginForm" :rules="rules">
                  <el-form-item label="" prop="username">
                    <el-input size="large" v-model="loginForm.username" placeholder="请输入账号">
                      <template #prefix>
                        <el-icon><User /></el-icon>
                      </template>
                    </el-input>
                  </el-form-item>
                  <el-form-item label="" prop="password">
                    <el-input  size="large" v-model="loginForm.password" :type="isHide ? 'password' :'text'" placeholder="请输入密码">
                      <template #prefix>
                        <el-icon><Lock /></el-icon>
                      </template>
                      <template #suffix>
                        <el-icon @click="isHide = !isHide">
                          <View v-if="isHide"/>
                          <Hide v-else/>
                        </el-icon>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-form>
                <el-button size="large" style="width: 100%" type="primary" @click="login">登录</el-button>
                <div  style="text-align: right">
                  <el-button type="text" @click="toRegister">立即注册</el-button>
                </div>
              </el-tab-pane>
<!--              <el-tab-pane label="验证码登录" name="second">-->
<!--                <el-form ref="loginFormRef2" label-position="top" size="large" :model="loginForm2" :rules="rules2">-->
<!--                  <el-form-item label="" prop="phone">-->
<!--                    <el-input clearable v-model="loginForm2.phone" placeholder="请输入手机号">-->
<!--                      <template #prefix>-->
<!--                        <el-icon><Iphone /></el-icon>-->
<!--                      </template>-->
<!--                    </el-input>-->
<!--                  </el-form-item>-->
<!--                  <el-form-item label="" prop="code">-->
<!--                    <div class="f-r a-c" style="width: 100%">-->
<!--                      <div class="f-g">-->
<!--                        <el-input clearable v-model="loginForm2.code" placeholder="请输入验证码">-->
<!--                          <template #prefix>-->
<!--                            <el-icon><Lock /></el-icon>-->
<!--                          </template>-->
<!--                          <template #suffix>-->
<!--                            <el-button type="text">获取验证码</el-button>-->
<!--                          </template>-->
<!--                        </el-input>-->
<!--                      </div>-->
<!--                    </div>-->
<!--                  </el-form-item>-->
<!--                </el-form>-->
<!--                <el-button size="large" style="width: 100%" type="primary" @click="login2">登录</el-button>-->
<!--                <div  style="text-align: right">-->
<!--                  <el-button type="text" @click="toRegister">立即注册</el-button>-->
<!--                </div>-->
<!--              </el-tab-pane>-->
            </el-tabs>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background-image: url("~@/assets/login_bg.png");
  background-size: 100% 100%;
  background-position: center center;
  background-repeat: no-repeat;
  overflow: auto;
}
.login-form {
  .login-title {
    font-family: 优设标题黑;
    font-size: 52px;
    font-weight: normal;
    line-height: 72px;
    text-align: center;
    letter-spacing: 0em;
    color: #FFFFFF;
    width: 575px;
    height: 43px;
    margin: 100px 0;
  }
}
.login-form-box{
  width: 930px;
  /* 矩形 217 */
  border-radius: 8px;
  background: #FFFFFF;
  padding: 29px 38px;
  .name  {
    font-size: 24px;
    color: #333333;
    margin-bottom: 27px;
    text-align: left;
    width: 100%;
    font-weight: bold;
  }
  .el-form {
    width: 100%;
    :deep(.el-input) .el-input__wrapper {
      background-color: #ECECEC;
    }
  }
  .form-content {
    width: 100%;
    flex-grow: 1;
    padding-right: 100px;
    .form-icon {
      background-image: url("~@/assets/login_icon.png");
      background-size: 100% 100%;
      background-repeat: no-repeat;
      width: 347px;
      height: 347px;
    }
  }
}
</style>